<template>
  <div class="registration-page">
    <el-card class="form-card">
      <h2>驾考报名单</h2>
      <el-form :model="formData" :rules="rules" ref="registrationForm" label-width="120px">
        <el-form-item label="用户ID" prop="user_id">
          <el-input v-model="formData.user_id" disabled></el-input>
        </el-form-item>

        <el-form-item label="驾照类型" prop="registration_type">
          <el-select v-model="formData.registration_type" placeholder="请选择驾照类型">
            <el-option label="C1驾照" value="C1"></el-option>
            <el-option label="C2驾照" value="C2"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="首选时段" prop="preferred_time">
          <el-radio-group v-model="formData.preferred_time">
            <el-radio label="上午">上午（8:00-12:00）</el-radio>
            <el-radio label="下午">下午（14:00-18:00）</el-radio>
            <el-radio label="晚上">晚上（19:00-21:00）</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="报名日期" prop="registration_date">
          <el-date-picker
            v-model="formData.registration_date"
            type="date"
            placeholder="选择报名日期"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </el-form-item>

        <el-form-item label="备注信息" prop="notes">
          <el-input
            type="textarea"
            v-model="formData.notes"
            :rows="3"
            placeholder="可填写特殊需求或备注信息"
          ></el-input>
        </el-form-item>

        <el-form-item label="当前状态">
          <el-tag :type="statusType">{{ statusText }}</el-tag>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm">提交报名</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'RegistrationPage',
  data() {
    return {
      formData: {
        user_id: '', // 实际应从用户信息获取
        registration_type: '',
        preferred_time: '',
        registration_date: '',
        notes: '',
        status: 0
      },
      rules: {
        registration_type: [
          { required: true, message: '请选择驾照类型', trigger: 'change' }
        ],
        preferred_time: [
          { required: true, message: '请选择首选时段', trigger: 'change' }
        ],
        registration_date: [
          { required: true, message: '请选择报名日期', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    statusType() {
      const typeMap = {
        0: 'info',
        1: 'warning',
        2: 'success',
        3: 'danger'
      }
      return typeMap[this.formData.status]
    },
    statusText() {
      const textMap = {
        0: '未提交',
        1: '待审核',
        2: '已通过',
        3: '已退回'
      }
      return textMap[this.formData.status]
    }
  },
  methods: {
    submitForm() {
      this.$refs.registrationForm.validate(async (valid) => {
        if (valid) {
          try {
            // 提交到后端API
            await this.$axios.post('/api/registration', {
              ...this.formData,
              status: 1 // 提交后状态变更为待审核
            })
            this.$message.success('报名单提交成功')
            this.formData.status = 1
          } catch (error) {
            this.$message.error('提交失败，请重试')
          }
        } else {
          this.$message.error('请完善必填信息')
          return false
        }
      })
    },
    resetForm() {
      this.$refs.registrationForm.resetFields()
      this.formData.status = 0
    }
  }
}
</script>

<style scoped>
.registration-page {
  padding: 20px;
}

.form-card {
  max-width: 800px;
  margin: 0 auto;
}

h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #303133;
}

.el-radio {
  margin-right: 20px;
}
</style>